package com.xuecheng.basic.controller;

import com.xuecheng.basic.domain.Dictionary;
import com.xuecheng.commons.model.vo.DictionaryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xuecheng.basic.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 数据字典前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryService  dictionaryService;

    /**
     * 查询所有的数据字典
     * @return 数据字典 集合
     */
    @GetMapping
    public ResponseResult findAll(){
        // 查询所有数据字典
        List<Dictionary> dictionaryList = dictionaryService.list();
        // 对象转换
        List<DictionaryVo> dictionaryVoList = BeanHelper.copyWithCollection(dictionaryList, DictionaryVo.class);
        // 返回值
        return ResponseResult.okResult(dictionaryVoList);
    }
    /**
     * 根据code查询指定的数据字典
     * @param code
     * @return
     */
    @GetMapping("/{code}")
    public ResponseResult findByCode(@PathVariable("code") String code ){
        return  dictionaryService.findByCode(code);
    }
}
